<script>
    $(document).ready(function () {
        //添加
        $("#add-sub").click(function () {
            var platfm_id = $("#release-platfm").val();
            var release_host = $("#release-host").val();
            var proxy_host = $("#proxy-host").val();
            $.ajax({
                type: "POST",
                async: false,
                url: "/item/addrelease/",
                data: {
                    "platfm_id": platfm_id,
                    "release_host": release_host,
                    "proxy_host": proxy_host,
                },
                dataType: "json",
                success: function (data) {
                    $("#addModal").modal('hide');
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()", 2100);
                },
                error: function (data) {
                    spop({
                        template: data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            })
        });

        //获取修改内容
        $("td>a[name='edit-release']").click(function () {
            var id = $(this).attr("release_id");
            $.ajax({
                type: "POST",
                async: false,
                url: "/item/idrelease/",
                data: {"id": id},
                dataType: "json",
                success: function (data) {
                    $("#edit-release-platfm").val(data.PlatfmID);
                    $("#edit-release-host").val(data.ReleaseHost);
                    $("#edit-proxy-host").val(data.ProxyHost);
                    $("#edit-test-url").val(data.TestUrl);
                    $("#edit-sub").attr("release_id", data.ID);
                    $("#editModal").modal("show");
                },
                error: function (data) {
                    spop({
                        template: data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            });
        });

        //提交修改内容
        $("#edit-sub").click(function () {
            var id = $(this).attr("release_id");
            var platfm_id = $("#edit-release-platfm").val();
            var release_host = $("#edit-release-host").val();
            var proxy_host = $("#edit-proxy-host").val();
            $.ajax({
                type: "PUT",
                async: false,
                url: "/item/editrelease/",
                data: {
                    "id": id,
                    "platfm_id": platfm_id,
                    "release_host": release_host,
                    "proxy_host": proxy_host,
                },
                dataType: "json",
                success: function (data) {
                    $("#editModal").modal('hide');
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()", 2100);
                },
                error: function (data) {
                    spop({
                        template: data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            })
        });

        //删除
        $("td>a[name='del-release']").click(function () {
            var id = $(this).attr('release_id');
            var statu = confirm("是否确认删除!");
            if (statu == true) {
                $.ajax({
                    type: "post",
                    async: false,
                    url: "/item/delrelease/",
                    data: {"id": id},
                    dataType: "json",
                    success: function (data) {
                        spop({
                            template: data,
                            style: 'warning',
                            autoclose: 2000
                        });
                        setTimeout("location.reload()", 2100);
                    },
                    error: function (data) {
                        spop({
                            template: data.responseText,
                            style: 'error',
                            autoclose: 2000
                        });
                    }
                });
            }
        });

        //搜索
        $("#search-key").bind('change', function () {
            var key = $(this).val();
            if (key != "") {
                var url = "/item/searchrelease/" + key + "/";
            } else {
                var url = "/item/release/";
            }
            $("#search-a").attr("href", url);
        });

        //同步版本
        $("td>a[name='code-vs']").click(function () {
            var id = $(this).attr('release_id');
            $.ajax({
                type: "post",
                async: false,
                url: "/item/syncvs/",
                data: {"id": id},
                dataType: "json",
                success: function (data) {
                    spop({
                        template: data,
                        style: 'warning',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()", 2100);
                },
                error: function (data) {
                    spop({
                        template: data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            });

        });


        //发布
        $("td>a[name='code-release']").click(function () {
            var id = $(this).attr('release_id');
            var cur_version = $(this).parent().siblings("td[name='vs']").children().text();
            var statu = confirm("当前版本：{ " + cur_version + " } 是否发布？");
            if (statu == true) {
                $.ajax({
                    type: "post",
                    async: false,
                    url: "/item/coderelease/",
                    data: {"id": id},
                    dataType: "json",
                    success: function (data) {
                        spop({
                            template: data,
                            style: 'success',
                            autoclose: 2000
                        });
                        setTimeout("location.reload()", 2100);
                    },
                    error: function (data) {
                        spop({
                            template: data.responseText,
                            style: 'error',
                            autoclose: 2000
                        });
                    }
                });
            }
        });

        //热更
        $("td>a[name='code-hotup']").click(function () {
            var id = $(this).attr('release_id');
            $("#hotupModal").modal("show");
            $("#hotup-sub").attr("release_id", id)
        });


        function check_str(str) {
            var objRegExp = /\s+|\;+|\&+|\|+|[\r\n]/g;
            ret = str.replace(objRegExp, "");
            console.log(ret);
            return ret
        }



        $("#hotup-sub").click(function () {
            var formData = new FormData();
            var id = $(this).attr('release_id');
            var hot_file = $("#hotup-file").val();
            var hot_file_path = $("#hotup-file-path").val();
            var hot_file_str = check_str(hot_file);
            var upload_file = $("#upload-file").get(0).files[0];

            formData.append("id", id);
            formData.append("hot_file", hot_file_str);
            formData.append("hot_file_path", hot_file_path);
            formData.append("upload_file", upload_file);
            if (hot_file_str != "" || upload_file) {
                $.ajax({
                    type: "post",
                    processData: false,
                    contentType: false,
                    url: "/item/codehotup/",
                    data: formData,
                    success: function (data) {
                        $("#hotupModal").modal("hide");
                        spop({
                            template: data,
                            style: 'success',
                            autoclose: 5000
                        });
                        setTimeout("location.reload()", 5100);
                    },
                    error: function (data) {
                        spop({
                            template: data.responseText,
                            style: 'error',
                            autoclose: 2000
                        });
                    }
                });
            } else {
                spop({
                    template: "热更内容不正确",
                    style: 'warning',
                    autoclose: 2000
                });
            }
        });

        //内测服更新
        $("td>a[name='up-test']").click(function () {
            var id = $(this).attr('test_id');
            var statu = confirm("是否更新内测服？");
            if (statu == true) {
                $.ajax({
                    type: "post",
                    async: false,
                    url: "/item/testup/",
                    data: {"id": id},
                    dataType: "json",
                    success: function (data) {
                        spop({
                            template: data,
                            style: 'success',
                            autoclose: 2000
                        });
                        setTimeout("location.reload()", 2100);
                    },
                    error: function (data) {
                        spop({
                            template: data.responseText,
                            style: 'error',
                            autoclose: 2000
                        });
                    }
                });
            }
        });

    })
</script>
